package com.jcrj.service;

import com.jcrj.dao.QuickMenuDao;
import com.jcrj.entity.OFile;
import com.jcrj.entity.OMenu;
import com.jcrj.entity.OMenuQuick;
import java.util.List;

public class QuickMenuService
{
  private QuickMenuDao quickMenuDao;
  private StringBuffer buff;

  public String getImgId()
  {
    this.buff = new StringBuffer();
    String hql = "from OFile where foreign=4";
    List list = this.quickMenuDao.findByHql(hql);
    int cc = 0;
    for (int i = 0; i < list.size(); i++) {
      if (cc == 0) {
        this.buff.append("<tr>");
      }
      this.buff.append("<td align='center'><img src='" + ((OFile)list.get(i)).getUrl() + "' width='30' height='30'><br><input id='imgId' name='imgId' value='" + ((OFile)list.get(i)).getUrl() + "' type='radio' ></td>");
      cc++;
      if (cc == 5) {
        this.buff.append("</tr>");
        cc = 0;
      } else if (i == list.size() - 1) {
        this.buff.append("</tr>");
      }
    }
    return this.buff.toString();
  }

  public String getimg()
  {
    this.buff = new StringBuffer();
    String hql = "from OFile where foreign=4";
    List list = this.quickMenuDao.findByHql(hql);
    int cc = 0;
    for (int i = 0; i < list.size(); i++) {
      if (cc == 0) {
        this.buff.append("<tr>");
      }
      this.buff.append("<td align='center'><img src='" + ((OFile)list.get(i)).getUrl() + "' width='30' height='30'><br><input id='imgId' name='imgId' value='" + ((OFile)list.get(i)).getId() + "' type='checkbox' ></td>");
      cc++;
      if (cc == 5) {
        this.buff.append("</tr>");
        cc = 0;
      } else if (i == list.size() - 1) {
        this.buff.append("</tr>");
      }
    }
    return this.buff.toString();
  }

  public List<OMenuQuick> getMenuQuickList(int userId) {
    String hql = "from OMenuQuick where myUserId=" + userId + " order by px asc";
    return this.quickMenuDao.findByHql(hql);
  }

  public void updateMenu(int userId, String[] menuId, String[] menuName, String[] px, String[] imgUrl) {
    String hql = "delete from OMenuQuick where myUserId=" + userId;
    this.quickMenuDao.executeHql(hql);
    for (int i = 0; i < menuId.length; i++)
      if ((menuId[i] != null) && (!"".equals(menuId[i]))) {
        OMenuQuick menuQuick = new OMenuQuick();
        menuQuick.setMenuId(Integer.valueOf(Integer.parseInt(menuId[i])));
        menuQuick.setPx(Integer.valueOf(Integer.parseInt(px[i])));
        menuQuick.setMyUserId(Integer.valueOf(userId));
        menuQuick.setExtend(menuName[i]);
        menuQuick.setImgfile(imgUrl[i]);
        this.quickMenuDao.saveQuick(menuQuick);
      }
  }

  public void saveMenu(int userId, String[] menuId, String[] menuName, String[] px, String[] imgUrl)
  {
    for (int i = 0; i < menuId.length; i++)
      if ((menuId[i] != null) && (!"".equals(menuId[i]))) {
        OMenuQuick menuQuick = new OMenuQuick();
        menuQuick.setMenuId(Integer.valueOf(Integer.parseInt(menuId[i])));
        menuQuick.setPx(Integer.valueOf(Integer.parseInt(px[i])));
        menuQuick.setMyUserId(Integer.valueOf(userId));
        menuQuick.setExtend(menuName[i]);
        menuQuick.setImgfile(imgUrl[i]);
        this.quickMenuDao.saveQuick(menuQuick);
      }
  }

  public boolean getMenuIf(int userId)
  {
    String hql = "from OMenuQuick where myUserId=" + userId;

    return this.quickMenuDao.findByHql(hql).size() != 0;
  }

  public String getMenuAll(int userid)
  {
    this.buff = new StringBuffer();
    try {
      String hql = "from OMenu where id>1 and pid=0 order by px asc";
      List<OMenu> list = this.quickMenuDao.findByHql(hql);
      for (OMenu menu : list) {
        this.buff.append("<tr><td style='font-size: 14px;color: #00BFFF'>" + menu.getName() + "：<td></tr>");
        getMenuSon(menu.getId().intValue(), userid);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return this.buff.toString();
  }

  public String getMenuSon(int id, int userid) {
    String hql = "from OMenu where pid in(select o.id from OMenu o where o.pid=" + id + ") and id in(select t.menuId from ORoleMenu t where t.roleId in(select m.roleId from ORoleUser m where m.userId=" + userid + ")))";
    List<OMenu> list = this.quickMenuDao.findByHql(hql);
    this.buff.append("<tr>");
    for (OMenu menu : list) {
      this.buff.append("<td style='border-bottom: 1px solid #999;'><input value='" + menu.getId() + "_" + menu.getName() + "' type='radio' name='radioMenu'>" + menu.getName() + "</td>");
    }
    this.buff.append("</tr>");
    return this.buff.toString();
  }

  public QuickMenuDao getQuickMenuDao()
  {
    return this.quickMenuDao;
  }

  public void setQuickMenuDao(QuickMenuDao quickMenuDao) {
    this.quickMenuDao = quickMenuDao;
  }
}

/* Location:           /Volumes/work/oa/WEB-INF/classes/
 * Qualified Name:     com.jcrj.service.QuickMenuService
 * JD-Core Version:    0.6.0
 */